package com.ikkong.sn.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ikkong.common.vo.ShiroUser;
import com.ikkong.core.base.BaseController;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.jfinal.ext.shiro.ClearShiro;
import com.ikkong.core.jfinal.ext.shiro.ShiroKit;
import com.ikkong.core.toolbox.Record;
import com.ikkong.sn.model.SnOrderIn;
import com.ikkong.sn.model.SnSupplier;
import com.ikkong.system.controller.base.UrlPermissController;
import com.ikkong.sn.model.SnOrderPay;
import com.ikkong.sn.service.SnOrderPayService;
import com.ikkong.sn.service.impl.SnOrderPayServiceImpl;
import org.beetl.sql.core.kit.StringKit;

/**
 * Generated by Blade.
 * 2019-04-25 21:36:08
 */
public class SnOrderPayController extends UrlPermissController {
	private static String CODE = "snOrderPay";
	private static String PERFIX = "sn_order_pay";
	private static String LIST_SOURCE = "SnOrderPay.list";
	private static String BASE_PATH = "/platform/snOrderPay/";
	
	SnOrderPayService service = new SnOrderPayServiceImpl();
	
	public void index() {
		String s = ShiroKit.getUser().getRoleList().get(0);
		ShiroUser user = ShiroKit.getUser();
		setAttr("deptid",ShiroKit.getUser().getDeptId());
		setAttr("role",s);
		setAttr("code", CODE);
		render(BASE_PATH + "snOrderPay.html");
	}

	public void add() {
		List<SnSupplier> suppliers = Blade.create(SnSupplier.class).findAll();
		SnOrderPay snOrderPay = new SnOrderPay();
		snOrderPay.setApply_date(new Date());
		snOrderPay.setStatus(-1);
		snOrderPay.setDept_id((Integer)ShiroKit.getUser().getDeptId());

		snOrderPay.setU_id((Integer)ShiroKit.getUser().getId());
		int id = service.saveRtId(snOrderPay);
		setAttr("suppliers",suppliers);
		setAttr("deptname",ShiroKit.getUser().getDeptName());
		setAttr("deptid",ShiroKit.getUser().getDeptId());
		setAttr("orderId",id);
		setAttr("code", CODE);
		render(BASE_PATH + "snOrderPay_add.html");
	}
	@ClearShiro
	public void pass(){
		String status = getPara("status");
		if(StringKit.isBlank(status)){
			renderJson(error("没有状态 值"));
			return;
		}
		String orderId = getPara("orderId");
		if(StringKit.isBlank(orderId)){
			renderJson(error("未传入单号"));
			return;
		}

		Map para = new HashMap();
		para.put("status",status);
		para.put("orderId",orderId);
		String opstep1 = getPara("opstep1");
		String opstep2 = getPara("opstep2");
		String opstep3 = getPara("opstep3");
		String opstep4 = getPara("opstep4");
		if(StringKit.isNotBlank(opstep1)){
			para.put("opstep1",opstep1);
		}
		if(StringKit.isNotBlank(opstep2)){
			para.put("opstep2",opstep2);
		}
		if(StringKit.isNotBlank(opstep3)){
			para.put("opstep3",opstep3);
		}
		if(StringKit.isNotBlank(opstep4)){
			para.put("opstep4",opstep4);
		}
		int update = Blade.dao().update("SnOrderPay.updateStatus", para);
		if(update>0){
			renderJson(success("操作成功"));
		}else {
			renderJson(error("操作失败"));
		}
	}

	public void edit() {
		String id = getPara(0);
		setAttr("orderId",id);
		setAttr("deptname",ShiroKit.getUser().getDeptName());
		setAttr("deptid",ShiroKit.getUser().getDeptId());
		Map snOrderPay = Blade.dao().selectSingle("SnOrderPay.findById", Record.create().set("id",id),Map.class);
		setAttr("order",snOrderPay);
		setAttr("model", JsonKit.toJson(snOrderPay));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "snOrderPay_edit.html");
	}

	public void view() {
		String id = getPara(0);
		setAttr("orderId",id);
		setAttr("deptname",ShiroKit.getUser().getDeptName());
		setAttr("deptid",ShiroKit.getUser().getDeptId());
		Map snOrderPay = Blade.dao().selectSingle("SnOrderPay.findById", Record.create().set("id",id),Map.class);
		setAttr("order",snOrderPay);
		setAttr("model", JsonKit.toJson(snOrderPay));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "snOrderPay_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE);
		renderJson(grid);
	}

	public void save() {
		SnOrderPay snOrderPay = mapping(PERFIX, SnOrderPay.class);
		boolean temp = service.save(snOrderPay);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		SnOrderPay snOrderPay = mapping(PERFIX, SnOrderPay.class);
		boolean temp = service.update(snOrderPay);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
}
